/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import entity.Cell;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Arena;
import model.Robot;

/**
 *
 * @author minhtue001
 */
public class SensorHandler implements Handler {

    @Override
    public void handle(String command) {
        
        // The origin of the Robot is at (x, y)
        int x = Robot.getInstance().getX();
        int y = Robot.getInstance().getY();
        x += 1;
        y += 1;
        
        int SFL = 350;
        int SFR = 350;
        int SFM = 8;
        int SL = 300;
        int SR = 0;
        
                

        if (command.substring(0, 3).equals("SFL")) {
            int a = x;
            int b = y;
            a += Robot.dx[Robot.getInstance().getDirection()];
            b += Robot.dy[Robot.getInstance().getDirection()];
            //the midle front cell is at (a, b)
            
            int d = (Robot.getInstance().getDirection() + 3) % 4;
            a += Robot.dx[d];
            b += Robot.dy[d];
            
            //left front cell is at (a, b)
            
            int len = Integer.parseInt(command.substring(3));
            if (len < SFL) return;
            len = 1;
            a += len * Robot.dx[Robot.getInstance().getDirection()];
            b += len * Robot.dy[Robot.getInstance().getDirection()];
            

            Arena.getInstance().addObstacleCell(new Cell(a, b));
        }
        else  if (command.substring(0, 3).equals("SFM")) {
            int a = x;
            int b = y;
            a += Robot.dx[Robot.getInstance().getDirection()];
            b += Robot.dy[Robot.getInstance().getDirection()];
            // midle front cell is at (a, b)
            
            int len = Integer.parseInt(command.substring(3));
            if (len > SFM) return;
            len = (len / 10) + 1;
            
            a += len * Robot.dx[Robot.getInstance().getDirection()];
            b += len * Robot.dy[Robot.getInstance().getDirection()];
            
            Arena.getInstance().addObstacleCell(new Cell(a, b));
            
        }
        else  if (command.substring(0, 3).equals("SFR")) {
                 int a = x;
            int b = y;
            a += Robot.dx[Robot.getInstance().getDirection()];
            b += Robot.dy[Robot.getInstance().getDirection()];
            // midle front cell is at (a, b)
            
            int d = (Robot.getInstance().getDirection() + 1) % 4;
            a += Robot.dx[d];
            b += Robot.dy[d];
            
            //left front cell is at (a, b)
            
            int len = Integer.parseInt(command.substring(3));
            if (len < SFR) return;
            len = 1;
            a += len * Robot.dx[Robot.getInstance().getDirection()];
            b += len * Robot.dy[Robot.getInstance().getDirection()];
            
            Arena.getInstance().addObstacleCell(new Cell(a, b));
            
        }
        else  if (command.substring(0, 2).equals("SL")) {
            int a = x;
            int b = y;
            a += Robot.dx[Robot.getInstance().getDirection()];
            b += Robot.dy[Robot.getInstance().getDirection()];
            // midle front cell is at (a, b)
            
            int d = (Robot.getInstance().getDirection() + 3) % 4;
            a += Robot.dx[d];
            b += Robot.dy[d];
            
            //left sensor is at (a, b)
            
            int len = Integer.parseInt(command.substring(2));
            if (len < SL) return;
            len = 1;
            
            a += len * Robot.dx[d];
            b += len * Robot.dy[d];
            
            Arena.getInstance().addObstacleCell(new Cell(a, b));
            
        }
        else  if (command.substring(0, 2).equals("SR")) {
            if (true) return;
            int a = x;
            int b = y;
            a -= Robot.dx[Robot.getInstance().getDirection()];
            b -= Robot.dy[Robot.getInstance().getDirection()];
            // midle front cell is at (a, b)
            
            int d = (Robot.getInstance().getDirection() + 1) % 4;
            a += Robot.dx[d];
            b += Robot.dy[d];
            
            //left sensor is at (a, b)
            
            int len = Integer.parseInt(command.substring(2));
            if (len > SR) return;
            len = (len / 10) + 1;
            
            a += len * Robot.dx[d];
            b += len * Robot.dy[d];
            
            Arena.getInstance().addObstacleCell(new Cell(a, b));
        }
        else try {
            throw(new Exception("Invalid Sensor Command"));
        } catch (Exception ex) {
            Logger.getLogger(SensorHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
